package cn.edu.sgu.www.mhxysy.controller.trade;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.trade.TradeRecordDTO;
import cn.edu.sgu.www.mhxysy.entity.trade.TradeRecord;
import cn.edu.sgu.www.mhxysy.pager.trade.TradeRecordPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.trade.TradeRecordService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "交易记录管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/trade_record", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class TradeRecordController {

    private final TradeRecordService tradeRecordService;

    @Autowired
    public TradeRecordController(TradeRecordService tradeRecordService) {
        this.tradeRecordService = tradeRecordService;
    }

    @ApiOperation("添加交易记录")
    @ApiOperationSupport(ignoreParameters = "id")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated TradeRecordDTO tradeRecordDTO) {
        tradeRecordService.insert(tradeRecordDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID删除交易记录")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public JsonResult<Void> deleteById(
            @ApiParam(name = "id", value = "交易记录ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        tradeRecordService.deleteById(id);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改交易记录信息")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated TradeRecordDTO tradeRecordDTO) {
        tradeRecordService.updateById(tradeRecordDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("分页查询交易记录列表")
    @RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
    public JsonResult<JsonPage<TradeRecord>> selectByPage(TradeRecordPager pager) {
        Page<TradeRecord> page = tradeRecordService.selectByPage(pager);

        return JsonResult.restPage(page);
    }

}